package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.config.CurrentUserContextHolders;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.businesses.BusinessDTO;
import com.qk.dto.businesses.BusinessQueryDTO;
import com.qk.dto.businesses.BusinessTrackRecordDTO;
import com.qk.dto.businesses.UpdateBusinessDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.entity.User;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import com.qk.vo.business.GetBusinessTrackRecordVO;
import com.qk.vo.clue.GetClueTrackRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author kaoM1Jy
 * @date 2025/10/15 11:09
 * @description:
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;


    @Override
    public PageResult<GetBusinessTrackRecordVO> getBusinessPool(BusinessTrackRecordDTO dto) {
        Page pages = new Page<>(dto.getPage(), dto.getPageSize());
        Page<GetBusinessTrackRecordVO> pageResult =this.baseMapper.selectLists(pages, dto);
        return PageResult.<GetBusinessTrackRecordVO>builder()
                .total(Long.valueOf(pages.getTotal()).intValue())
                .rows(pageResult.getRecords())
                .build();
    }

    @Transactional
    @Override
    public void updateBusinessInfoById(UpdateBusinessDTO dto) {
       Business business = BeanUtil.copyProperties(dto, Business.class);
       business.setUpdateTime(LocalDateTime.now());
       if(business.getStatus()==BusinessStatusConstants.WAIT_FOLLOW_UP){
           business.setStatus(BusinessStatusConstants.FOLLOW_UP);
       }
       this.updateById(business);
       BusinessTrackRecord businessTrackRecord = BeanUtil.copyProperties(dto, BusinessTrackRecord.class);
       businessTrackRecord.setId(null);
       businessTrackRecord.setCreateTime(LocalDateTime.now());
       businessTrackRecord.setBusinessId(business.getId());
       businessTrackRecord.setUserId(CurrentUserContextHolders.get());
       this.businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public BusinessVO getBusinessById(Integer id) {
        Business business = this.getById(id);
        BusinessVO businessVO = BeanUtil.copyProperties(business, BusinessVO.class);
        Integer userId = business.getUserId();
        User user = this.userMapper.selectById(userId);
        List<BusinessTrackRecord> businessTrackRecords = businessTrackRecordMapper.selectList(Wrappers.lambdaQuery(BusinessTrackRecord.class)
                .in(BusinessTrackRecord::getBusinessId, id));

        List<BusinessVO.BusinessTrackRecordVO> list = businessTrackRecords.stream().map(item -> {
            BusinessVO.BusinessTrackRecordVO vo = BeanUtil.copyProperties(item, BusinessVO.BusinessTrackRecordVO.class);
            vo.setAssignName(user.getName());
            return vo;
        }).toList();
        businessVO.setTrackRecords(list);
        return businessVO;
    }

    @Override
    public void toCustomer(Integer id) {
        Business business = this.getById(id);
        business.setStatus(BusinessStatusConstants.CONVERT_TO_CUSTOMER);
        business.setUpdateTime(LocalDateTime.now());
        this.updateById(business);
        Customer customer = BeanUtil.copyProperties(business, Customer.class);
        customer.setId(null);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customer.setBusinessId(id);
        this.customerMapper.insert(customer);

    }

    @Override
    public PageResult<BusinessDO> getBusinessList(BusinessQueryDTO dto) {
        Page pages = new Page<>(dto.getPage(), dto.getPageSize());
        Page<BusinessDO> page = this.baseMapper.getBusinessList(pages, dto);
        return PageResult.<BusinessDO>builder()
                .total(Long.valueOf(pages.getTotal()).intValue())
                .rows(page.getRecords())
                .build();

    }

    @Override
    public void addBusiness(BusinessDTO dto) {
        if(ObjectUtil.isEmpty(dto.getName())||ObjectUtil.isEmpty(dto.getPhone())||ObjectUtil.isEmpty(dto.getChannel())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Business business = BeanUtil.copyProperties(dto, Business.class);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        this.save(business);
    }
}

